package nju.bishe.weibo.adapter;

import java.util.List;

import nju.bishe.weibo.R;
import nju.bishe.weibo.TwitterApplication;
import nju.bishe.weibo.data.Tweet;
import nju.bishe.weibo.helper.GetBitmapFromURL;
import nju.bishe.weibo.helper.ProfileImageCacheCallback;
import nju.bishe.weibo.helper.Utils;

import weibo4andriod.Status;
import android.content.Context;
import android.graphics.Bitmap;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * 
 * @author longwei.wu
 * @version $Id: LaughStatusAdapter.java
 */
public class LaughStatusAdapter extends BaseAdapter {
    
    private final Context context;
    private final List<Status> status;
    protected StringBuilder mMetaBuilder;
    protected LayoutInflater mInflater;

    
    /**
     * @param context
     * @param status
     */
    public LaughStatusAdapter(Context context, List<Status> status) {
        super();
        this.context = context;
        this.status = status;
        mMetaBuilder= new StringBuilder();
        mInflater = LayoutInflater.from(context);
    }

    /** 
     * @see android.widget.Adapter#getCount()
     */
    @Override
    public int getCount() {
        return this.status.size();
    }

    /** 
     * @see android.widget.Adapter#getItem(int)
     */
    @Override
    public Object getItem(int position) {
        return status.get(position);
    }

    /** 
     * @see android.widget.Adapter#getItemId(int)
     */
    @Override
    public long getItemId(int position) {
        return position;
    }
	private static class ViewHolder {
		public TextView tweetUserText;
		public TextView tweetText;
		public ImageView profileImage;
		public TextView metaText;
		public ImageView fav;
		public ImageView has_image;
	}

    /** 
     * @see android.widget.Adapter#getView(int, android.view.View, android.view.ViewGroup)
     */
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
//        Status statu = status.get(position);
//        return new StatusListView(this.context,statu);
		View view;

		
		if (convertView == null) {
			view = mInflater.inflate(R.layout.tweet, parent, false);

			ViewHolder holder = new ViewHolder();
			holder.tweetUserText = (TextView) view
					.findViewById(R.id.tweet_user_text);
			holder.tweetText = (TextView) view.findViewById(R.id.tweet_text);
			holder.profileImage = (ImageView) view
					.findViewById(R.id.profile_image);
			holder.metaText = (TextView) view
					.findViewById(R.id.tweet_meta_text);
			holder.fav = (ImageView) view.findViewById(R.id.tweet_fav);
			holder.has_image = (ImageView) view.findViewById(R.id.tweet_has_image);
			view.setTag(holder);
		} else {
			view = convertView;
		}

		ViewHolder holder = (ViewHolder) view.getTag();
		
		Status tweet = status.get(position);

		holder.tweetUserText.setText(tweet.getUser().getScreenName());
		Utils.setSimpleTweetText(holder.tweetText, tweet.getText());
		// holder.tweetText.setText(tweet.text, BufferType.SPANNABLE);


//		holder.profileImage.setImageURI(Uri.parse(tweet.getUser().getProfileImageURL().toString()));
		  String profileImageUrl = tweet.getUser().getProfileImageURL().toString();

	
			if (!Utils.isEmpty(profileImageUrl)) {
				holder.profileImage.setImageBitmap(GetBitmapFromURL.getBytesFromImageURL(tweet.getUser().getProfileImageURL()));
			}
		

		holder.metaText.setText(Tweet.buildMetaText(mMetaBuilder,
				tweet.getCreatedAt(), tweet.getSource(), tweet.getInReplyToScreenName()));

		if (tweet.isFavorited()) {
			holder.fav.setVisibility(View.VISIBLE);
		} else {
			holder.fav.setVisibility(View.GONE);
		}
		
		if (!Utils.isEmpty(tweet.getThumbnail_pic())) {
			holder.has_image.setVisibility(View.VISIBLE);
		} else {
			holder.has_image.setVisibility(View.GONE);
		}

		return view;
    }
//    private final class StatusListView extends LinearLayout {
//
//        private ImageView userImage;
//        private TextView text;
//        public StatusListView(Context context, Status statu) {
//
//            super(context);
//            this.setOrientation(LinearLayout.HORIZONTAL);
//
//            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
//                ViewGroup.LayoutParams.WRAP_CONTENT);
//            params.setMargins(5, 3, 5, 0);
//            
//            this.userImage = new ImageView(context);
//            this.userImage.setImageURI(Uri.parse(statu.getUser().getProfileImageURL().toString()));
//            this.addView(userImage);
//                        
//              this.text = new TextView(context);
//              this.text.setText(statu.getText());
//              this.addView(text);
//            
//        }
//    }
    
    public  void refresh(){
    	
    }
}
